const Router = require('koa-router')
const router = new Router()
const callCloudDB = require('../utils/callCloudDB.js')
const cloudStorage = require('../utils/callCloudStorage.js')

router.get('/list', async (ctx, next) => {
    const params = ctx.request.query
    const query = `
        db.collection('blog').skip(${params.start}).limit(${params.count}).orderBy('createTime','desc').get()
    `
    const res = await callCloudDB(ctx, 'databasequery', query)
    // console.log(res)

    //文件下载链接
    let fileList = []
    const data = res.data
    for (let i = 0, len = data.length; i < len; i++) {
        for (let j = 0, leng = JSON.parse(data[i]).img.length; j < leng; j++)
            fileList.push({
                fileid: JSON.parse(data[i]).img[j],
                max_age: 7200
            })
    }
    // for (let i = 0, len = data.length; i < len; i++) {
    //     fileList.push({
    //         fileid: JSON.parse(data[i]).img[0],
    //         max_age: 7200
    //     })
    // }
    const dlRes = await cloudStorage.download(ctx, fileList)
    // console.log(dlRes)
    let returnData = []
    let i = 0
    let g = 0

    let len = dlRes.file_list.length
    // console.log(len)
    // console.log(data.length)
    // console.log(i)
    while (i < len) {
        let downloadlist = []

        let j = 0
        for (j; j < JSON.parse(data[g]).img.length; j++) {
            downloadlist.push(
                dlRes.file_list[i + j].download_url,
            )
        }

        i += j

        returnData.push({
            download_url: downloadlist,
            _id: JSON.parse(data[g])._id,
            _openid: JSON.parse(data[g])._openid,
            avatarUrl: JSON.parse(data[g]).avatarUrl,
            content: JSON.parse(data[g]).content,
            createTime: JSON.parse(data[g]).createTime,
            nickName: JSON.parse(data[g]).nickName,
            img: JSON.parse(data[g]).img,
            state: JSON.parse(data[g]).state,
            headline: JSON.parse(data[g]).headline
        })
        ++g
    }


    ctx.body = {
        data: returnData,
        code: 20000,
    }
})

router.post('/del', async (ctx, next) => {
    const params = ctx.request.body
    // 删除blog
    const queryBlog = `db.collection('blog').doc('${params._id}').remove()`
    const delBlogRes = await callCloudDB(ctx, 'databasedelete', queryBlog)

    // 删除blog-comment
    const queryComment = `db.collection('blog-comment').where({
        postId: '${params._id}'
    }).remove()`
    const delCommentRes = await callCloudDB(ctx, 'databasedelete', queryComment)

    // 删除图片
    const delStorageRes = await cloudStorage.delete(ctx, params.img)
    ctx.body = {
        code: 20000,
        data: {
            delBlogRes,
            delCommentRes,
            delStorageRes
        }
    }
})

//下架
router.post('/off', async (ctx, next) => {
    const params = ctx.request.body
    // console.log(params)
    const query = `
            db.collection('blog').doc('${params._id}').update({
                data: {
                    state: ${params.state},
                }
            })
        `
    const res = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        code: 20000,
        data: res
    }
})

//查看详情
router.get('/comment', async (ctx, next) => {
    const params = ctx.request.query.id
    const commentlist = `db.collection('blog-comment').where({ postId: '${params}' }).get()`
    const CommentRes = await callCloudDB(ctx, 'databasequery', commentlist)
    ctx.body = {
        data: CommentRes,
        code: 20000,
    }
})

module.exports = router